<?php
require_once(dirname(__FILE__).'/config.php');
CheckPurview('a_New,a_AccNew');
require_once(DEDEINC.'/customfields.func.php');
require_once(DEDEADMIN.'/inc/inc_archives_functions.php');
if(file_exists(DEDEDATA.'/template.rand.php'))
{
    require_once(DEDEDATA.'/template.rand.php');
}
if(empty($dopost)) $dopost = '';

if($dopost=='save')
{
    if(empty($body))
    {
        ShowMsg("请填写隐藏内容！","-1");
        exit();
    }
    if(!TestPurview('a_New'))
    {
        CheckCatalog($typeid,"对不起，你没有操作栏目 {$typeid} 的权限！");
    }

    $adminid = $cuserLogin->getUserID();
    $aid = intval($aid);

    //处理body字段自动摘要、自动提取缩略图等
    $description = '';
    $litpic = '';
    $keywords = '';
    $body = AnalyseHtmlBody($body,$description,$litpic,$keywords,'htmltext');

    //自动分页
//    if($sptype=='auto')
//    {
//        $body = SpLongBody($body,$spsize*1024,"#p#分页标题#e#");
//    }

    if($aid){
        $price = floatval($price);
        $arcrank = intval($arcrank);
        $hidden = getHiden($aid);
        if(empty($hidden)){
            $query = "INSERT INTO `#@__hiddens`(aid,price,body,created_at) VALUES ('$aid','$price','$body','".date('Y-m-d H:i:s')."');";
        }else{
            $query = "UPDATE `#@__hiddens` set `body` = '{$body}',`price` = '{$price}',`arcrank` = '{$arcrank}' where aid = {$aid}";
        }
        if(!$dsql->ExecuteNoneQuery($query))
        {
            ShowMsg("保存失败！","javascript:;");
            exit();
        }
    }else{
        $aid = getLastArcId();
        //保存到主表
        $query = "INSERT INTO `#@__hiddens`(aid,price,body,created_at) VALUES ('$aid','$price','$body','".date('Y-m-d H:i:s')."');";
        if(!$dsql->ExecuteNoneQuery($query))
        {
            ShowMsg("保存失败！","javascript:;");
            exit();
        }
    }
    ShowMsg("保存成功！","javascript:;");
}else if($dopost=='delete'){
    if($aid){
        $query = "DELETE FROM `#@__hiddens` where aid = {$aid}";
        if(!$dsql->ExecuteNoneQuery($query))
        {
            ShowMsg("删除失败！","javascript:;");
            exit();
        }
    }else{
        $aid = getLastArcId();
        $query = "DELETE FROM `#@__hiddens` where aid = {$aid}";
        if(!$dsql->ExecuteNoneQuery($query))
        {
            ShowMsg("删除失败！","javascript:;");
            exit();
        }
    }
    ShowMsg("删除成功！","javascript:;");
}else{
    require_once(DEDEINC."/dedetag.class.php");
    require_once(DEDEADMIN."/inc/inc_catalog_options.php");
    ClearMyAddon();
    $channelid = empty($channelid) ? 0 : intval($channelid);
    $cid = empty($cid) ? 0 : intval($cid);

    if(empty($geturl)) $geturl = '';

    $keywords = $writer = $source = $body = $description = $title = '';

    //采集单个网页
    if(preg_match("#^http:\/\/#", $geturl))
    {
        require_once(DEDEADMIN."/inc/inc_coonepage.php");
        $redatas = CoOnePage($geturl);
        extract($redatas);
    }

    //获得频道模型ID
    if($cid>0 && $channelid==0)
    {
        $row = $dsql->GetOne("Select channeltype From `#@__arctype` where id='$cid'; ");
        $channelid = $row['channeltype'];
    }
    else
    {
        if($channelid==0)
        {
            $channelid = 1;
        }
    }
    $aid = $aid ? intval($aid) : getLastArcId();
    $addRow = $dsql->GetOne(" Select * From  `#@__hiddens` where aid='$aid' ");
    $addRow['aid'] = $aid;
    include DedeInclude("templets/hidden_edit.htm");
    exit();
}
function getLastArcId()
{
    global $dsql;
    $iquery = "SELECT id from `#@__arctiny` order by id desc limit 1";
    $row = $dsql->GetOne($iquery);
    return $row['id']+1;
}
function getHiden($aid)
{
    global $dsql;
    $row = $dsql->GetOne("SELECT * FROM `#@__hiddens` WHERE aid='{$aid}' ");
    return $row;
}
?>